package tp.java.model;

import java.util.Observable;

import tp.java.model.tp3.Pile2;
import tp.java.model.tp3.PileI;
import tp.java.model.tp3.PilePleineException;
import tp.java.model.tp3.PileVideException;

public class Calculette extends Observable implements CalculetteI {
	private PileI<Integer> pile;

	public Calculette(){
		this.pile = new Pile2<Integer>(PileI.CAPACITE_PAR_DEFAUT);
	}

	public Calculette(PileI<Integer> pile){
		this.pile = pile;
	}
	public void enter(int i) throws CalculetteException {
		try{
			pile.empiler(i);
			setChanged(); notifyObservers(i);
		}catch(PilePleineException e){
			throw new CalculetteException(e.getMessage());
		}
	}

	public int result() throws CalculetteException {
		try{
			return pile.sommet();
		}catch(PileVideException e){
			throw new CalculetteException(e.getMessage());
		}
	}

	public int pop() throws CalculetteException {
		try{
			return pile.depiler();
		}catch(PileVideException e){
			throw new CalculetteException(e.getMessage());
		}
	}


	public void add() throws CalculetteException {
		try{
			int op2 = pile.depiler();
			int op1 = pile.depiler();
			pile.empiler(op1 + op2);
			setChanged();notifyObservers();
		}catch(PilePleineException e){
			throw new CalculetteException(e.getMessage());
		}catch(PileVideException e){
			throw new CalculetteException(e.getMessage());
		}
	}

	public void sub() throws CalculetteException {
		try{
			int op2 = pile.depiler();
			int op1 = pile.depiler();
			pile.empiler(op1 - op2);
			setChanged();notifyObservers();
		}catch(PilePleineException e){
			throw new CalculetteException(e.getMessage());
		}catch(PileVideException e){
			throw new CalculetteException(e.getMessage());
		}
	}

	public void div() throws CalculetteException {
		// TODO Auto-generated method stub
		try {
			int a = pile.depiler();
			int b = pile.depiler();
			if(a == 0) {
				pile.empiler(b); 
				pile.empiler(a);
			}
			else pile.empiler(b/a);
		} catch (PilePleineException e) {
			// TODO Auto-generated catch block
			throw new CalculetteException(e.getMessage());
		} catch (PileVideException e) {
			// TODO Auto-generated catch block
			throw new CalculetteException(e.getMessage());
		}
		setChanged(); notifyObservers();
	}

	public void mul() throws CalculetteException {
		// TODO Auto-generated method stub
		try {
			pile.empiler(pile.depiler() * pile.depiler());
		} catch (PilePleineException e) {
			// TODO Auto-generated catch block
			throw new CalculetteException(e.getMessage());
		} catch (PileVideException e) {
			// TODO Auto-generated catch block
			throw new CalculetteException(e.getMessage());
		}
		setChanged(); notifyObservers();
	}

	public void clear(){
		while(!pile.estVide()){
			try{
				pile.depiler();
			}catch(PileVideException e){	
			}
		}
		setChanged(); notifyObservers();
	}

	public String toString(){
		return pile.toString();
	}
	public boolean isEmpty(){
		return pile.estVide();
	}
	public boolean isFull(){
		return pile.estPleine();
	}
	public int size(){
		return pile.taille();
	}
	public int capacity(){
		return pile.capacite();
	}

}
